package org.jslaughter.level;

import org.jslaughter.camera.plugin.SHMShakyCamEffect;
import org.jslaughter.camera.plugin.SmoothFollowPlugin;
import org.jslaughter.component.inventory.WeaponManager;
import org.jslaughter.event.eventtype.DeathEvent;
import org.jslaughter.event.eventtype.DeathListener;
import org.newdawn.slick.Font;
import org.nvframe.camera.Camera;
import org.nvframe.entity.Entity;
import org.nvframe.event.EventService;
import org.nvframe.event.eventtype.RenderEvent;
import org.nvframe.event.eventtype.RenderListener;
import org.nvframe.exception.NVFrameException;
import org.nvframe.manager.ConfigManager;
import org.nvframe.manager.EntityManager;
import org.nvframe.manager.ResourceManager;

public class Level1 extends Level implements DeathListener, RenderListener {
	
	private Font verdana;
	
	public void start() {
		Entity player = EntityManager.getInstance().getEntity("ent_player");
		
		WeaponManager weaponManager = (WeaponManager) player.getComponent(WeaponManager.class);
		weaponManager.addWeapon("ent_weapon_pistol");
		
		// add a camera that follows player
		Camera camera = Camera.getInstance();
		camera.addPlugin(new SHMShakyCamEffect(player));
		camera.addPlugin(new SmoothFollowPlugin(player));
		
		// prefetch fonts for later use
		try {
			verdana = ResourceManager.getInstance().getFont("fnt_verdana_15");
		} catch (NVFrameException e) {
			e.printStackTrace();
		}
		
		EventService.getInstance().addEventListener(this);
	}

	@Override
	public void onDeath(DeathEvent event) {
		
		// player death?
		if(event.getEntity().getId().equals("ent_player"))
			System.out.println("GAME OVER");
		
		/*int spawnerCount = EntityManager.getInstance().getInstancesCount("ent_enemyspawner1", true);
		int enemyCount = EntityManager.getInstance().getInstancesCount("ent_enemy1", true);
		
		// entity removed bug => death gets fired before entity is removed
		// TODO fix this bug!!!
		if(event.getEntity() == EntityManager.getInstance().getEntity("ent_enemyspawner1") && event.getEntity().isEnabled())
			spawnerCount--;
		if(event.getEntity() == EntityManager.getInstance().getEntity("ent_enemy1") && event.getEntity().isEnabled())
			enemyCount--;
		
		System.out.println("spawner(s) left: " + spawnerCount);
		System.out.println("enemie(s) left: " + enemyCount);

		
		if(spawnerCount <= 0 && enemyCount <= 0) {
			verdana.drawString(10, 400, "YOU WON!");
			System.out.println("YOU WON!");
		}*/
	}

	public void onRender(RenderEvent event) {
		// WORKS!
		/*Camera.getInstance().untranslateGraphics(event.getGc());
		if(death)
			verdana.drawString(400, 400, "GAME OVER MAN, YOU FAILED BIG TIME!");
		Camera.getInstance().translateGraphics(event.getGc());*/
		
		if(ConfigManager.getInstance().getActive("debugMode"))
			verdana.drawString(10, 40, "DEBUG MODE! Some nifty keys: \n\n" +
					"F1 - switch debugmode on/off \n" +
					"F2 - vsync \n" +
					"F3 - draw physics bodies (still has a few bugs :p) \n" +
					"F4 - draw entity positions \n" +
					"F5 - print entitylist in console \n" +
					"F6 - sound on/off \n" +
					"F7 - print all keybindings in console \n" +
					"P - pauzes the game \n" +
					"L SHIFT - run, will be removed in productionversion");
	}
	
}
